package com.qst.order.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    // 定义交换机、队列名称
    public static final String DELAYED_EXCHANGE = "delayed.exchange";
    public static final String DELAYED_QUEUE = "order-pay-queue";
    public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";

    /**
     * 声明延迟交换机（类型为 x-delayed-message）
     */
    @Bean
    public CustomExchange delayedExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct"); // 底层路由策略（与普通direct交换机一致）
        return new CustomExchange(
                DELAYED_EXCHANGE,
                "x-delayed-message", // 延迟交换机类型
                true,   // 是否持久化
                false,  // 是否自动删除
                args
        );
    }

    /**
     * 声明延迟队列
     */
    @Bean
    public Queue delayedQueue() {
        return QueueBuilder.durable(DELAYED_QUEUE).build();
    }

    /**
     * 绑定队列到交换机
     */
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(delayedQueue())
                .to(delayedExchange())
                .with(DELAYED_ROUTING_KEY)
                .noargs(); // 延迟交换机无需额外参数
    }
}